<?php
require 'Model.php';
class CarphotoModel extends Model{
		function __Construct(){
			parent::__Construct();
		}
		function getCarphotoList(){
			$sql = 'select * from carphoto';
			$data = $this->conn->query($sql);
			//$this->conn = new ConnDB();
			return $data;
		}


		function getCarphotoByID($id){
			$sql = "select * from carphoto where cid=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}

		function Updatecarphoto($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}
		  	$carphoto= array(
				'cid' =>$_POST['cid'],
				'bid' =>$_POST['bid'],
				'tid' =>$_POST['tid'],
				'carname' => $_POST['carname'],
				'intro' => $_POST['intro'],
				'uploaddate' => $_POST['uploaddate'],
				'pic' => $pic,
				);
			$sql="update carphoto set cid='$carphoto[cid]',bid='$carphoto[bid]',tid='$carphoto[tid]',carname='$carphoto[carname]',intro='$carphoto[intro]',uploaddate='$carphoto[uploaddate]',pic='$carphoto[pic]' where cid=".$carphoto['cid'];
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function addCarphoto($data){
			//var_dump($data);
			//如果选择了照片，则上传照片文件
			if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}
			$carphoto = array(
				'cid' =>'',
				'bid' => $data['bid'],
				'tid' => $data['tid'],
				'carname' => $data['carname'],
				'intro' => $data['intro'],
				'uploaddate' => $data['uploaddate'],
				'pic' => $pic,
				);
			$keys = implode(',',array_keys($carphoto));
			$values = implode("','",array_values($carphoto));
			/*$sql = "insert into carphoto values ('$carphoto[cid]','$carphoto[bid]','$carphoto[tid]','$carphoto[carname]','$carphoto[intro]','$carphoto[uploaddate]','$carphoto[pic]')";Z*/
			$sql="insert into carphoto ({$keys}) VALUES ('{$values}')";
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function Delcarphoto($id){
			$sql = "Delete from carphoto where cid=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}
	}
 ?>